﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelephoneYiShou.XQL;

namespace TelephoneYiShou
{
    public partial class LoginFrm : Skin_Mac
    {
        public LoginFrm()
        {
            InitializeComponent();
            //skinEngine1.SkinFile = Application.StartupPath + @"\Warm.ssk";//皮肤
        }
        public string name;//姓名
        public string pwd;//密码
        public string dept;//部门
        private void login_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“telephoneYiShouDataSet.Employee”中。您可以根据需要移动或删除它。
            this.employeeTableAdapter.Fill(this.telephoneYiShouDataSet.Employee);
            loginDept.Items.Insert(0, "管理员");
            loginDept.Items.Insert(1, "销售员");
        }
        private void pictureBox7_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        //登录
        private void buttlogin_Click_1(object sender, EventArgs e)
        {
            name = loginname.Text;
            pwd = loginPwd.Text;
            dept = loginDept.Text;
            if (string.IsNullOrEmpty(name))
            {
                MessageBoxEx.Show("请输入姓名");
            }
            //判断密码是否为空
            else if (string.IsNullOrEmpty(dept))
            {
                MessageBoxEx.Show("请输入职务");
            }
            else if (string.IsNullOrEmpty(pwd))
            {
                MessageBoxEx.Show("请输入密码");
            }
            //管理员登录
            else if (dept == "管理员")
            {
                //判断姓名是否为空
                //如果都不为空
                DBHelper.InitConnection();
                string ssql = string.Format("select EmployeeName,Pwd from Employee where EmployeeName='{0}' and pwd='{1}' and dept='{2}'", name, pwd, dept);//SQL命令
                SqlDataReader sdr = DBHelper.GetDataReader(ssql);//判断是否有数据
                if (sdr.HasRows)
                {
                    sdr.Close();
                    FrmMain fm = new FrmMain(name);//登入主窗体
                    fm.Show();
                    loginPwd.Text = null;
                    loginname.Text = null;
                }
                else
                {
                    sdr.Close();
                    loginPwd.Text = null;
                    loginname.Text = null;
                    MessageBoxEx.Show("密码，账号或职务错误");
                }
            }
            //销售员登录
            else if (dept == "销售员")
            {
                //判断姓名是否为空
                if (string.IsNullOrEmpty(name))
                {
                    MessageBoxEx.Show("请输入姓名");
                }
                //判断密码是否为空
                else if (string.IsNullOrEmpty(pwd))
                {
                    MessageBoxEx.Show("请输入密码");
                }
                //如果都不为空
                else
                {
                    DBHelper.InitConnection();
                    string ssql = string.Format("select EmployeeName,Pwd from Employee where EmployeeName='{0}' and dept='{2}'", name, pwd, dept);//SQL命令
                    SqlDataReader sdr = DBHelper.GetDataReader(ssql);//判断是否有数据
                    if (sdr.HasRows)
                    {
                        sdr.Close();
                        FrmSaleMain fsm = new FrmSaleMain(name);  //登入主窗体
                        fsm.ShowDialog();
                    }
                    else
                    {
                        sdr.Close();
                        loginPwd.Text = null;
                        loginname.Text = null;
                        MessageBoxEx.Show("密码，账号或职务错误");
                    }

                }
            }
        }

        private void skinPictureBox1_Click(object sender, EventArgs e)
        {
            if (skinPictureBox1.Visible==true)
            {
                skinPictureBox1.Visible = false;
                skinPictureBox2.Visible = true;
            }
            loginPwd.IsPasswordChat = '*';
        }

        private void skinPictureBox2_Click(object sender, EventArgs e)
        {
            if (skinPictureBox2.Visible == true)
            {
                skinPictureBox2.Visible = false;
                skinPictureBox1.Visible = true;
            }
            loginPwd.IsPasswordChat = default;
        }
    }
}
